package com.ht.bean;

import java.util.ArrayList;
import java.util.List;

public class ProductResultCopy {

	public static final String SUCCESS = "success";
	public static final String FAIL = "fail";

	public static String getSuccessResult(String message, List<Product> products) {
		String json = "{'result':'" + SUCCESS + "','message':'" + message + "'";
		if (products != null) {
			return json + ",'products':" + listToJSON(products) + "}";
		}
		return json + "}";
	}

	public static String getSuccessResult(String message, Product product) {
		List<Product> ps = new ArrayList<>();
		ps.add(product);
		return getSuccessResult(message, ps);
	}

	public static String getFailResult(String message) {
		return "{'result':'" + FAIL + "','message':'" + message + "'}";
	}

	private static String listToJSON(List<Product> products) {
		String jsonData = "[";
		for (Product p : products) {
			jsonData += "{'id':'" + p.getId() + "','name':'" + p.getName() + "','price':" + p.getPrice() + "},";
		}
		jsonData = jsonData.substring(0, jsonData.length() - 1);
		jsonData += "]";
		return jsonData;
	}

}
